package irio.gamer;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Logic.GamerParams;

import utils.JVector;
import utils.Vec;

public class SettingsPanel extends JPanel {
	
	private int returnVal;
	
	
	public static boolean verifyMpiRun() {
		File f = new File(gamer.gamerParams.mpiCommand);
		if(f.exists()) return true;
		return false;
	}
	
	public JPanel mpiPanel() {
		JPanel pnl = new JPanel();
		pnl.setLayout(new GridLayout(3, 3));
		final JTextField file = new JTextField(gamer.gamerParams.mpiCommand);
		//file.setEditable(false);
		JButton find = new JButton("Locate file");

		file.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				gamer.gamerParams.mpiCommand = file.getText().trim();
				gamer.verifyMpirun();
			}
		});
		
		find.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fc = new JFileChooser();
				fc.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
							System.out.println("Returnval: "+ returnVal);
					        if (returnVal == JFileChooser.APPROVE_OPTION) {
					        	gamer.gamerParams.mpiCommand = fc.getSelectedFile().getAbsolutePath();
					        	file.setText(gamer.gamerParams.mpiCommand);
					        	//gamer.galaxy.Load(fc.getSelectedFile().getName());
					        	//GalaxyComponentPanel.cpl.reload();
								//gamer.imagePanel.preview.resetPreview();

					            
					            //This is where a real application would open the file.
					   }
					}
				});
		       returnVal  = fc.showOpenDialog(gamer.galaxyPanel);

					
			}
		});
		
		
		pnl.add(new JLabel(""));
		pnl.add(new JLabel(""));
		pnl.add(new JLabel(""));

		pnl.add(new JLabel("Location of mpirun"));
		pnl.add(file);
		pnl.add(find);

		pnl.add(new JLabel(""));
		pnl.add(new JLabel(""));
		pnl.add(new JLabel(""));

		return pnl;
	}
	
	
	public SettingsPanel() {
		setLayout(new GridLayout(5, 1));

		add(new JVector("Wavelengths (BRG)", gamer.gamerParams.wavelengths, new Vec(0,0,0), new Vec(10000, 10000, 10000), GamerParams.integer));
		add(mpiPanel());

		add(new JLabel(""));
		add(new JLabel(""));

		add(new JLabel(""));
}

}
